package com.info.threads;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.info.dbase.Connections;
import com.info.dbase.ExecuteSql;
import com.info.server.Event;
import com.info.server.StartServer;
import com.info.utils.StringTools;

public class GetPutInfoThread extends Thread {

	private static Logger logger = Logger.getLogger(GetPutInfoThread.class);
	public static HashMap hm = null;
	private ExecuteSql execute = null;
	boolean runflag = false;

	public GetPutInfoThread() {
		if (hm == null) {
			hm = new HashMap();
		}
	}

	public void extuteSql() throws Exception {
		if (this.execute == null)
			this.execute = new ExecuteSql();
		ResultSet rs = null;
		String sql = null;
		String content = "";
		Connection conn = null;
		Statement stmt = null;
		Event et = null;
		try {
			conn = Connections.getConnection();
			//43
			sql = "select terminal,typeno,RESERVATION,contents0x from t_gps_setting where status =0";
			rs = this.execute.getRs(sql, conn,stmt);
			while (rs.next()) {
				et = new Event();
				et.setTerID(rs.getString("terminal"));
				et.setProNum(rs.getString("typeno"));
				et.setReservation(rs.getString("RESERVATION"));
				et.setContents0x( rs.getString("contents0x"));
//				et.setReservation(rs.getString("reservation"));
				if (! (content.length() > 0)) {
					et.setContents0x("0A");
				}
				hm.put(et.getTerID(), et);
			}
			//83
			sql = "select terminal,typeno,serialno,lengths,target,latitude,longitude,statusbits from t_target_location where status =0";
			rs = this.execute.getRs(sql, conn,stmt);
			while (rs.next()) {
				et = new Event();
				et.setTerID(rs.getString("terminal"));
				et.setProNum(rs.getString("typeno"));
				et.setSerNum(StringTools.leftAdding(
						Integer.toHexString(rs.getInt("serialno")), "0", 4));
				et.setInfoLength(rs.getString("lengths"));
				et.setTargetCode(rs.getString("target"));
				et.setLatitude(rs.getString("latiude"));
				et.setLongitude(rs.getString("longitude"));
				et.setStatusBits(rs.getString("statusbits"));
				hm.put(et.getTerID(), et);
			}
			//upper
			sql = "select terminal,typeno,direction,lengths,contents0x from t_upper_info where status =0";
			rs = this.execute.getRs(sql, conn,stmt);
			while (rs.next()) {
				et = new Event();
				et.setTerID(rs.getString("terminal"));
				et.setProNum(rs.getString("typeno"));
				et.setDirection(rs.getString("direction"));
				et.setInfoLength(rs.getString("lengths"));
				et.setContents(rs.getString("contents0x"));
				hm.put(et.getTerID(), et);
			}
		} catch (Exception e) {
			logger.info("SHW#" + sql);
			e.printStackTrace();
		} finally {
			if (rs != null)
				rs.close();
			if(stmt!=null)
			stmt.close();
			Connections.close(conn);
		}
	}

	public void run() {
		while (!this.runflag) {
			try {
				cleanHM();
				extuteSql();
				if(hm.size()>0)
				logger.info("SHW#初始化下发消息 "+hm.size()+" 条");
				Thread.sleep(StartServer.scanPutInfo);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void cleanHM() {
		if (hm != null) {
			hm.clear();
		}
	}
}
